package com.lge.pitchingcoins.util.lockkey;

import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

import java.lang.reflect.Field;

public class WindowManagerForKK implements IWindowManagerLayoutParams {
    private Field privateFlagsField = null;
    private LayoutParams layoutParams = null;
    
    public WindowManagerForKK(LayoutParams layoutParams) {
        try {
            privateFlagsField = WindowManager.LayoutParams.class.getDeclaredField("privateFlags");
        } catch (Exception e) {
            privateFlagsField = null;
        }
        
        this.layoutParams = layoutParams;
    }
    
    @Override
    public void addLayoutParamExtendFlag(int flag) {
        if (privateFlagsField != null && layoutParams != null) {
            try {
                int privateFlags = (int)privateFlagsField.getInt(layoutParams);
                privateFlags |= flag;
                privateFlagsField.setInt(layoutParams, privateFlags);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void setLayoutParamExtendFlag(int flag) {
        if (privateFlagsField != null && layoutParams != null) {
            try {
                privateFlagsField.setInt(layoutParams, flag);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int getLayoutParamExtendFlag() {
        if (privateFlagsField != null && layoutParams != null) {
            try {
                return privateFlagsField.getInt(layoutParams);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        return 0;
    }

    @Override
    public LayoutParams getAttributes() {
        return layoutParams;
    }

    @Override
    public boolean isExtendFieldAccessible() {
        if (privateFlagsField != null) {
            return true;
        }
        
        return false;
    }
}